package com.imy.alscript;


import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.virtual.GmsSupport;
import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.remote.InstalledAppInfo;
import com.imy.alscript.models.AppInfo;

import java.util.ArrayList;
import java.util.List;

public class InstallAPKActivity extends AppCompatActivity {
  private ListView mListView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_install_a_p_k);
    mListView = (ListView) findViewById(R.id.list_view);
    Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        List<PackageInfo> installAppInfo = VirtualCore.get().getInstallAppInfo();
        setData(installAppInfo);
        //List<InstalledAppInfo> installedApps = VirtualCore.get().getInstalledApps(0);
        //List<AppInfo> list =new ArrayList<>();
        //for (int i = 0; i < installedApps.size(); i++) {
        //  InstalledAppInfo installedAppInfo = installedApps.get(i);
        //  AppInfo appInfo = new AppInfo();
        //  appInfo.path=installedAppInfo.apkPath;
        //  appInfo.name=installedAppInfo.packageName;
        //  appInfo.packageName=installedAppInfo.packageName;
        //  list.add(appInfo);
        //}

      }
    });
    thread.start();
  }

  private void setData(final List<PackageInfo> list) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        InstallListAdapter listAdapter = new InstallListAdapter(getBaseContext(), list);
        mListView.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new BaseSimpleListAdapter.onItemClickListener<PackageInfo>() {
          @Override
          public void onItemClick(PackageInfo bean, View itemView, int position) {
            VirtualCore.get().launchApp(bean.packageName);

          }
        });
      }
    });
  }

  private List<AppInfo> convertPackageInfoToAppData(Context context, List<PackageInfo> pkgList, boolean fastOpen) {
    PackageManager pm = context.getPackageManager();
    List<AppInfo> list = new ArrayList<>(pkgList.size());
    String hostPkg = VirtualCore.get().getHostPkg();
    for (PackageInfo pkg : pkgList) {
      // ignore the host package
      if (hostPkg.equals(pkg.packageName)) {
        continue;
      }
      // ignore the System package
      if (isSystemApplication(pkg)) {
        continue;
      }
      ApplicationInfo ai = pkg.applicationInfo;
      String path = ai.publicSourceDir != null ? ai.publicSourceDir : ai.sourceDir;
      if (path == null) {
        continue;
      }
      AppInfo info = new AppInfo();
      info.packageName = pkg.packageName;
      info.fastOpen = fastOpen;
      info.path = path;
      info.icon = ai.loadIcon(pm);
      info.name = ai.loadLabel(pm);
      InstalledAppInfo installedAppInfo = VirtualCore.get().getInstalledAppInfo(pkg.packageName, 0);
      if (installedAppInfo != null) {
        info.cloneCount = installedAppInfo.getInstalledUsers().length;
      }
      list.add(info);
    }
    return list;
  }

  private boolean isSystemApplication(PackageInfo packageInfo) {
    return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
        && !GmsSupport.isGmsFamilyPackage(packageInfo.packageName);
  }

}